package com.app.micaihu.ad.custom;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.app.micaihu.R;
import com.app.micaihu.ad.AdConfig;
import com.app.micaihu.app.AppApplication;
import com.app.micaihu.bean.ad.NativeAd;
import com.app.micaihu.configure.Config;
import com.app.micaihu.configure.UmEventId;
import com.app.micaihu.custom.components.downandupload.DownloadUtils;
import com.app.micaihu.custom.view.CustomImageView;
import com.app.micaihu.service.DownLoadService;
import com.app.micaihu.utils.MobclickAgentUtils;
import com.app.micaihu.view.main.game.GameDownloadActivity;
import com.app.okhttplib.OkHttpUtil;
import com.app.okhttplib.annotation.DownloadStatus;
import com.app.okhttplib.bean.DownloadFileInfo;
import com.app.utils.util.AppTools;
import com.app.utils.util.image.LoadImageUtils;
import com.baidu.mobstat.StatService;
import com.blankj.utilcode.util.AppUtils;

import java.io.File;
import java.util.HashMap;

/**
 * 游戏原生图片下载广告(适用于视频列表)
 * Created by linkai on 2016/2/10.
 */
public class GameImgNativeVideoView extends LinearLayout implements View.OnClickListener {

    public CustomImageView userHead;//用户头像
    public TextView userName;//用户昵称
    public CustomImageView img;//显示的大图片
    public TextView title;//标题

    private NativeAd nativeAd;


    private HashMap<String, Object> mapEvent;

    public GameImgNativeVideoView(Context context) {
        super(context);
        initView();
    }

    public GameImgNativeVideoView(Context context, HashMap<String, Object> mapEvent) {
        super(context);
        this.mapEvent = mapEvent;
        initView();
    }

    public void initView() {
        LayoutInflater.from(getContext()).inflate(R.layout.ad_game_img_videochannel, this);
        this.userHead = (CustomImageView) findViewById(R.id.video_usericon);
        this.userName = (TextView) findViewById(R.id.video_username);
        this.img = (CustomImageView) findViewById(R.id.thumb);
        this.title = (TextView) findViewById(R.id.item_public_title);
        this.userName.setOnClickListener(this);
        this.userHead.setOnClickListener(this);
        setOnClickListener(this);
    }

    /**
     * 设置即将加载的广告
     *
     * @param nativeAd
     */
    public void setNativeAd(NativeAd nativeAd) {
//        MobclickAgentUtils.event(Config.EventBauduId.game_exposed, "推荐");
        StatService.onEvent(getContext(), "game_exposed", "推荐", 1);//统计事件的点击次数
        if (nativeAd == null) {
            return;
        }
        this.title.setText(nativeAd.getTitle());
        if (nativeAd.getThumbList() != null && nativeAd.getThumbList().length >= 1) {
            LoadImageUtils.getInstance().loadImage(this.img, nativeAd.getThumbList()[0]);
        }
        LoadImageUtils.getInstance().loadImage(this.userHead, nativeAd.getAppIcon());
        this.userName.setText(nativeAd.getSummary());
        this.nativeAd = nativeAd;
        this.setTag("suces");//设置标识表示加载广告成功
    }


    @Override
    public void onClick(View v) {
        if (this.nativeAd == null) {
            return;
        }
        if (mapEvent != null) {
            MobclickAgentUtils.event(UmEventId.game_click, mapEvent);
            MobclickAgentUtils.event(UmEventId.game_download, mapEvent);
        }
        MobclickAgentUtils.event(Config.EventBauduId.game_076, "推荐");
        StatService.onEvent(getContext(), "067", "推荐", 1);//统计事件的点击次数
        String type;
        boolean appInstalled = AppUtils.isAppInstalled(nativeAd.getPackageName());
        if (appInstalled) {
            type = AdConfig.TYPE_2;
            com.app.okhttplib.util.AppUtils.startUpApp(getContext(), nativeAd.getIPackageName());
        } else {
            File unInstallFile = new File(DownloadUtils.DOWNLOAD_FILEDIR, nativeAd.getIPath() + ".apk");
            if (unInstallFile.exists() && unInstallFile.isFile()) {//安装游戏
                com.app.okhttplib.util.AppUtils.installAPP(getContext(), unInstallFile);
            } else {
                DownloadFileInfo downloadFileInfo = DownloadUtils.getSingleton().getDownloadFileInfo(OkHttpUtil.getTag(nativeAd.getIUrl()));
                if (downloadFileInfo == null) {
                    GameDownloadActivity.insertGame(nativeAd.getIGameId(), nativeAd.getITitle(), nativeAd.getIUrl(), nativeAd.getIAppIcon(),
                            nativeAd.getSummary(), nativeAd.getPackageName(), nativeAd.getIPath(), nativeAd.getISize());
                    DownloadUtils.getSingleton().down(nativeAd.getIUrl(), nativeAd.getIPath(), nativeAd);
                } else {
                    String downloadStatus = downloadFileInfo.getDownloadStatus();
                    if (!DownloadStatus.DOWNLOADING.equals(downloadStatus)) {
                        GameDownloadActivity.insertGame(nativeAd.getIGameId(), nativeAd.getITitle(), nativeAd.getIUrl(), nativeAd.getIAppIcon(),
                                nativeAd.getSummary(), nativeAd.getPackageName(), nativeAd.getIPath(), nativeAd.getISize());
                        DownloadUtils.getSingleton().down(nativeAd.getIUrl(), nativeAd.getIPath(), nativeAd);
                    }
                }
            }
            StatService.onEvent(getContext(), "057", "视频列表大图游戏下载", 1);// 统计的点击的次数
            type = AdConfig.TYPE_3;
        }
        String serialId = getTag(R.id.tag_first).toString();
        if (!TextUtils.isEmpty(serialId)) {
            AdConfig.staticsAd(serialId, type, AdConfig.AD_TYPE_GAME);
        }
    }

}
